package com.gitee.yannzh.rune.dao.base.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.gitee.yannzh.rune.core.base.pojo.AuditEntity;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * @author: yann
 * @description: 带有审计字段的数据实体
 * @date: 2023-12-20 17:04
 */

@Setter
@ToString
public abstract class BaseAuditEntity<T extends Model<T>, K extends Serializable, P extends Serializable> extends BaseEntity<T> implements AuditEntity<K, P, Integer> {
    private static final long serialVersionUID = 1L;

    /**
     * 创建人
     */
    @JsonIgnore
    @TableField(fill = FieldFill.INSERT, whereStrategy = FieldStrategy.NEVER)
    private P createBy;

    /**
     * 创建时间
     */
    @JsonIgnore
    @TableField(fill = FieldFill.INSERT, whereStrategy = FieldStrategy.NEVER)
    private LocalDateTime createTime;

    /**
     * 更新人
     */
    @JsonIgnore
    @TableField(fill = FieldFill.UPDATE, insertStrategy = FieldStrategy.NEVER)
    private P updateBy;

    /**
     * 更新时间
     */
    @JsonIgnore
    @TableField(fill = FieldFill.UPDATE, insertStrategy = FieldStrategy.NEVER)
    private LocalDateTime updateTime;

    /**
     * 版本号
     */
    @Version
    @TableField(fill = FieldFill.INSERT)
    private Integer version;
    /**
     * 逻辑删除
     */
    @Getter
    @JsonIgnore
    @TableLogic(delval = "id")
    @TableField(value = "is_deleted", select = false)
    private Long deleted;



    @Override
    public P getCreateBy() {
        return createBy;
    }

    @Override
    public LocalDateTime getCreateTime() {
        return createTime;
    }


    @Override
    public P getUpdateBy() {
        return updateBy;
    }

    @Override
    public LocalDateTime getUpdateTime() {
        return updateTime;
    }


    @Override
    public Integer getVersion() {
        return version;
    }

    @Override
    public boolean deletion() {
        return Objects.equals(getId(), deleted);
    }


}
